<?php

// +----------------------------------------------------------------------
// | ThinkAdmin
// +----------------------------------------------------------------------
// | 版权所有 2014~2019 广州楚才信息科技有限公司 [ http://www.cuci.cc ]
// +----------------------------------------------------------------------
// | 官方网站: http://demo.thinkadmin.top
// +----------------------------------------------------------------------
// | 开源协议 ( https://mit-license.org )
// +----------------------------------------------------------------------
// | gitee 代码仓库：https://gitee.com/zoujingli/ThinkAdmin
// | github 代码仓库：https://github.com/zoujingli/ThinkAdmin
// +----------------------------------------------------------------------

namespace app\wechat\controller;

use app\wechat\service\WechatService;
use library\Controller;
use think\Db;

/**
 * 微信数据统计
 * Class Index
 * @package app\wechat\controller
 */
class Index extends Controller
{
    /**
     * 微信数据统计
     * @auth true
     * @menu true
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     */
    public function index()
    {
        $map = ['appid' => WechatService::getAppid()];
        $this->totalJson = ['xs' => [], 'ys' => []];
        for ($i = 5; $i >= 0; $i--) {
            $time = strtotime("-{$i} months");
            $where = [['subscribe_at', '<', date('Y-m-32 00:00:00', $time)]];
            $this->totalJson['xs'][] = date('Y年m月', $time);
            $item = ['_0' => 0, '_1' => 0];
            $list = Db::name('WechatFans')->field('count(1) count,is_black black')->where($map)->where($where)->group('is_black')->select();
            foreach ($list as $vo) $item["_{$vo['black']}"] = $vo['count'];
            $this->totalJson['ys']['_0'][] = $item['_0'];
            $this->totalJson['ys']['_1'][] = $item['_1'];
        }
        $this->totalFans = Db::name('WechatFans')->where(['is_black' => '0'])->where($map)->count();
        $this->totalBlack = Db::name('WechatFans')->where(['is_black' => '1'])->where($map)->count();
        $this->totalNews = Db::name('WechatNews')->where(['is_deleted' => '0'])->count();
        $this->totalRule = Db::name('WechatKeys')->count();
        $this->fetch();
    }


    public function daochu(){
        $where = "where is_deleted=0";
        if(!empty($_GET['create_at'])){
            $create_at = $_GET['create_at'];
            $shijian = explode(' - ',$create_at);
            $start = $shijian[0].' 00:00:00';
            $end = $shijian[1].' 23:59:59';
            $where .= " and create_at >='$start' and create_at <='$end'";
        }

        if(!empty($_GET['user_id'])){
            $user_id = $_GET['user_id'];
            $where .= " and user_id ='$user_id'";
        }

        if(!empty($_GET['status'])){
            $status = $_GET['status'];
            $where .= " and status ='$status'";
        }

        $data = Db::query("select id,user_id,status,order_json,create_at,total_price,pay_amount,score,cancel_at from store_order_pay $where");
        foreach($data as $k =>&$v){
            $v['user'] = Db::table("store_member")->field("id,phone,username,nickname")->where(["id"=>$v['user_id']])->find();
            if($v['status']==0){ $v['status_name'] = '已取消'; }
            if($v['status']==1){ $v['status_name'] = '待付款'; }
            if($v['status']==2){ $v['status_name'] = '进行中'; }
            if($v['status']==3){ $v['status_name'] = '已完成'; }

            $v['order'] = json_decode($v['order_json'],true);
            $v['taocan'] = '';
            foreach ($v['order'] as $kk =>&$vv) {
                $vv['package'] = Db::table("store_package")->field("title")->where(["id" => $vv['package_id']])->where(["status" => 1, 'is_deleted' => 0])->find();
                $vv['project'] = Db::table("store_goods_cate")->field("title")->where("id", "in", $vv['project_id'])->select();

                $v['taocan'] .= ($kk+1).'、套餐名称：'.($vv['package']['title']).' ';

                $v['str'] = '';
                foreach ($vv['project'] as $k2 =>&$v2) {
                    $v['str'] .= '('.($k2+1).')'.'项目名称：'.($v2['title']).' ';
                }
                $v['taocan'] .= $v['str'].'。 ';
                $v['str'] = '';
            }
        }

        $xlsData = $data;

        require_once('vendor'.'/PHPExcel.php');
        $objPHPExcel = new \PHPExcel();
        // 设置sheet
        $objPHPExcel->setActiveSheetIndex(0);
        // 设置列的宽度
        $objPHPExcel->getActiveSheet()->getColumnDimension('A')->setWidth(15);
        $objPHPExcel->getActiveSheet()->getColumnDimension('B')->setWidth(20);
        $objPHPExcel->getActiveSheet()->getColumnDimension('C')->setWidth(20);
        $objPHPExcel->getActiveSheet()->getColumnDimension('D')->setWidth(20);
        $objPHPExcel->getActiveSheet()->getColumnDimension('E')->setWidth(20);
        $objPHPExcel->getActiveSheet()->getColumnDimension('F')->setWidth(20);
        $objPHPExcel->getActiveSheet()->getColumnDimension('G')->setWidth(150);
        $objPHPExcel->getActiveSheet()->getColumnDimension('H')->setWidth(20);

        // 设置表头
        $objPHPExcel->getActiveSheet()->SetCellValue('A1', '姓名');
        $objPHPExcel->getActiveSheet()->SetCellValue('B1', '手机号');
        $objPHPExcel->getActiveSheet()->SetCellValue('C1', '添加时间');
        $objPHPExcel->getActiveSheet()->SetCellValue('D1', '支付总金额');
        $objPHPExcel->getActiveSheet()->SetCellValue('E1', '支付积分');
        $objPHPExcel->getActiveSheet()->SetCellValue('F1', '订单状态');
        $objPHPExcel->getActiveSheet()->SetCellValue('G1', '订单详情');
        $objPHPExcel->getActiveSheet()->SetCellValue('H1', '取消时间');

        //存取数据
        $num = 2;
        foreach ($xlsData as $k => &$v) {

            $objPHPExcel->getActiveSheet()->SetCellValue('A' . $num, $v['user']['username'].' ');
            $objPHPExcel->getActiveSheet()->SetCellValue('B' . $num, $v['user']['phone'].' ');
            $objPHPExcel->getActiveSheet()->SetCellValue('C' . $num, $v['create_at'].' ');
            $objPHPExcel->getActiveSheet()->SetCellValue('D' . $num, $v['total_price'].'');
            $objPHPExcel->getActiveSheet()->SetCellValue('E' . $num, $v['score'].'');
            $objPHPExcel->getActiveSheet()->SetCellValue('F' . $num, $v['status_name'].'');
            $objPHPExcel->getActiveSheet()->SetCellValue('G' . $num, $v['taocan'].'');
            $objPHPExcel->getActiveSheet()->SetCellValue('H' . $num, $v['cancel_at'].'');

            $num++;
        }
        // 文件名称
        $fileName = "Excel" . date('Y-m-d', time()) . rand(1, 1000);
        $xlsName = iconv('utf-8', 'gb2312', $fileName);
        // 设置工作表名
        $objPHPExcel->getActiveSheet()->setTitle('sheet');
        //下载 excel5与excel2007
        $objWriter = new \PHPExcel_Writer_Excel5($objPHPExcel);
        ob_end_clean();     // 清除缓冲区,避免乱码
        header("Pragma: public");
        header("Expires: 0");
        header("Cache-Control:must-revalidate,post-check=0,pre-check=0");
        header("Content-Type:application/force-download");
        header("Content-Type:application/vnd.ms-execl;charset=UTF-8");
        header("Content-Type:application/octet-stream");
        header("Content-Type:application/download");
        header("Content-Disposition:attachment;filename=" . $xlsName . ".xls");
        header("Content-Transfer-Encoding:binary");
        $objWriter->save("php://output");




    }

}
